﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manageweb_Member_SetMemberXX : System.Web.UI.Page
{
    private String userName;

    protected void Page_Load(object sender, EventArgs e)
    {
        userName = Request["userID"];
        if (!Page.IsPostBack)
        {


            BindSingleUserInfo();
            BindUserXXUserInfo();
            BindXiaXianUserInfo();
        }
    }

    private bool isEnable = false;

    public bool IsEnable
    {
        get
        {
            return isEnable;
        }
    }

    private void BindSingleUserInfo()
    {
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable userTable = userAdp.GetSingleUserInfo(userName);
        SQLExpressDAL.Users.UsersRow userRow = null;

        if (userTable.Rows.Count > 0)
        {
            userRow = (SQLExpressDAL.Users.UsersRow)userTable.Rows[0];

            lblMemberAccount.Text = userRow.UserID;
            lblMemberName.Text = userRow.UserName;

        }
        else
        {
            return;
        }


    }

    private void BindXiaXianUserInfo()
    {
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable userTable = userAdp.GetXiaXianUserInfo(userName);


        this.GVUnassignXX.DataSource = userTable;
        this.GVUnassignXX.DataBind();

    }

    private void BindUserXXUserInfo()
    {
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable userTable = userAdp.GetUserXiaXianInfo(userName);


        this.GVAssingnXX.DataSource = userTable;
        this.GVAssingnXX.DataBind();

    }

    protected void lbtnDel_Command(object sender, CommandEventArgs e)
    {
        String userID = e.CommandArgument.ToString();
        String msg = "";

        try
        {
            SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
            int flag = userAdp.CancelXiaXianByUserID(userID);

            if (flag > 0)
            {
                msg = "删除成功!";
            }
            else
            {
                msg = "删除失败!";
            }
        }
        catch (Exception ex)
        {
            msg = "删除失败!";
        }
        BindUserXXUserInfo();
        BindXiaXianUserInfo();
        this.lblMsg.Text = msg;

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        String msg = "";
        try
        {
            String userID = Request["chksingle"];

            if (String.IsNullOrEmpty(userID))
            {
                msg = "未选择要设置上线的会员";
                return;
            }

            String[] userIDs = userID.Split(',');

            if (userIDs.Length > 2)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('下线会员数量选择过多');", true);
                return;
            }

            String parentID = this.lblMemberAccount.Text.Trim();

            SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
            int flag = Convert.ToInt32(userAdp.GetUserXXCount(parentID));


            if (flag < 2 && userIDs.Length != 0)
            {
                for (int i = 0; i < userIDs.Length; i++)
                {
                    if (flag < 2)
                    {
                        AddXiaXian(parentID, userIDs[i]);
                        flag++;
                    }
                }

            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('下线会员数量已设置为两个');", true);
                return;
            }
            msg = "设置成功.";
        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }
        finally
        {
            this.lblMsg.Text = msg;
        }

        BindUserXXUserInfo();
        BindXiaXianUserInfo();

    }

    protected void AddXiaXian(String parentID, String userIDs)
    {
        String msg = "";
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();

        int flag = userAdp.SetUserXX(parentID, userIDs);

        if (flag > 0)
        {
            msg = "设置成功.";
        }
        else
        {
            msg = "设置失败.";
        }

        this.lblMsg.Text = msg;

    }
}
